Skip to content

fix(runtime): flush pending telemetry at process exit - #108

Merged
kxzk merged 3 commits into
feature/aai-389-make-langfuse-background-threads-fork-safefrom
feature/aai-388-flush-pending-spans-and-scores-at-process-exit
Aug 17, 2026
Merged

fix(runtime): flush pending telemetry at process exit#108
kxzk merged 3 commits into
feature/aai-389-make-langfuse-background-threads-fork-safefrom
feature/aai-388-flush-pending-spans-and-scores-at-process-exit

Conversation

@kxzk

@kxzk kxzk commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

TL;DR

Normal process exit now flushes queued Langfuse spans and scores automatically.

Why

Short-lived tasks and one-off scripts could exit before SDK background workers delivered pending telemetry.

Checklist

  • Has label
  • Has linked issue
  • Tests added for new behavior
  • Docs updated (if user-facing)

Closes #

Note

Kade's words:

Summary

Normal process exit now flushes pending spans and scores. Applications do not need to register their own at_exit callback. Explicit Langfuse.shutdown and Langfuse.reset! disable the pending callback for the current lifecycle.

The callback runs once and catches shutdown errors. The callback inherits the fork-safe synchronization from the base PR. This PR targets the fork-safety branch so the unsafe exit-only behavior cannot land first.

Change diagram

flowchart LR
    accTitle: Automatic telemetry flush during process exit
    accDescr: A normal process exit runs one SDK callback that flushes queued spans and scores without allowing errors to escape.
    E["Normal process exit"] --> H["Run one SDK callback"]
    H --> T["Flush pending spans"]
    H --> S["Flush pending scores"]
    H --> G["Catch shutdown errors"]
    X["Explicit SDK shutdown"] --> D["Disable pending callback"]
Loading

Verification

I ran the complete RSpec suite locally. All 1,581 examples passed. Line coverage was 96.51%.

I ran RuboCop against 101 files. RuboCop reported no offenses.

I ran a separate Ruby process with the SDK and did not call Langfuse.shutdown. The process exited normally. The exit callback delivered trace 93999b473b9b9d18d22d0e1537004e64 and its queued sdk-exit-hook score. The Langfuse CLI read back both records.

I did not test abrupt termination because SIGKILL cannot run process-exit callbacks. I did not preview the rendered Mermaid diagram in GitHub. I reviewed the Mermaid source and syntax locally.


Note

Medium Risk
Changes global process exit and configuration assignment behavior; failures are contained but every configured process now attempts shutdown on exit.

Overview
Adds automatic flushing of pending spans and scores on normal process exit, so scripts and short-lived processes no longer need their own at_exit { Langfuse.shutdown }.

A new Langfuse::ExitHook registers a single Kernel.at_exit callback that runs Langfuse.shutdown once, swallows shutdown errors (warns instead), and resets its mutex after fork via ForkSafety. The hook is enabled when configuration is first built (configuration / configuration=) and disabled by explicit Langfuse.shutdown or Langfuse.reset! so double-flush is avoided. Assigning configuration= now calls reset! first and starts a fresh lifecycle.

CONFIGURATION.md documents the Process Exit behavior and updates the shutdown example away from manual at_exit.

Reviewed by Cursor Bugbot for commit f380f48. Bugbot is set up for automated code reviews on this repo. Configure here.

@kxzk kxzk added the bug Something isn't working label Aug 17, 2026
@linear-code

linear-code Bot commented Aug 17, 2026

Copy link
Copy Markdown

AAI-388

Comment thread lib/langfuse.rb Outdated
@kxzk
kxzk force-pushed the feature/aai-388-flush-pending-spans-and-scores-at-process-exit branch from 14dba71 to 6badd09 Compare August 17, 2026 14:41
@kxzk
kxzk force-pushed the feature/aai-388-flush-pending-spans-and-scores-at-process-exit branch from 6badd09 to 29cf976 Compare August 17, 2026 17:53
@kxzk
kxzk changed the base branch from feature/aai-389-make-langfuse-background-threads-fork-safe to main August 17, 2026 17:56
@kxzk kxzk closed this Aug 17, 2026
@kxzk kxzk reopened this Aug 17, 2026
@kxzk
kxzk changed the base branch from main to feature/aai-389-make-langfuse-background-threads-fork-safe August 17, 2026 17:58
@kxzk kxzk closed this Aug 17, 2026
@kxzk kxzk reopened this Aug 17, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 29cf976. Configure here.

Comment thread lib/langfuse.rb
@kxzk
kxzk changed the base branch from feature/aai-389-make-langfuse-background-threads-fork-safe to main August 17, 2026 18:30
@kxzk kxzk closed this Aug 17, 2026
@kxzk kxzk reopened this Aug 17, 2026
@kxzk
kxzk changed the base branch from main to feature/aai-389-make-langfuse-background-threads-fork-safe August 17, 2026 18:34
@kxzk kxzk closed this Aug 17, 2026
@kxzk kxzk reopened this Aug 17, 2026
@kxzk
kxzk merged commit 4cbdf3d into main Aug 17, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant